package com.headline.controller;

import com.headline.common.Log;
import com.headline.common.PageObject;
import com.headline.model.UserEntity;
import com.headline.service.UserService;
import com.sun.org.apache.bcel.internal.generic.RET;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by Administrator on 2016/10/11.
 */

@Controller
public class UsersController {

//    @Autowired
//    UserRepository userRepository;

    UserService userService = new UserService();

    Log log = Log.getLogger();

    @RequestMapping(value = "/admin/users", method = RequestMethod.GET)
    public String getUsers(HttpServletRequest request, ModelMap modelMap) {
        String pageNumberStr = request.getParameter("page");
        if (pageNumberStr == null || "".equals(pageNumberStr)) {
            pageNumberStr = "1";
        }
        int pageNumber = Integer.parseInt(pageNumberStr);
        int pageSize = 10;
        PageObject page = userService.queryPage("", pageNumber, pageSize);
        modelMap.addAttribute("userList", page.getDatasource());
        modelMap.addAttribute("totalPageNumber", page.getAbsolutePage());
        modelMap.addAttribute("pageSize", pageSize);
        modelMap.addAttribute("pageNumber", pageNumber);
        return "admin/users";
    }

    //get 添加用户请求
    @RequestMapping(value = "/admin/users/add", method = RequestMethod.GET)
    public String addUser() {
        return "admin/addUser";
    }

    /**
     * 添加用户
     * 2016/10/11  15:36
     */

    //post 处理用户请求
    @RequestMapping(value = "/admin/users/addP", method = RequestMethod.POST)
    public String addUserPost(@ModelAttribute("user") UserEntity userEntity) {
        userService.insertUser(userEntity);
        return "redirect:/admin/users";
    }

    /**
     * 查看用户详情
     * 2016/10/11  15:36
     */
    @RequestMapping(value = "/admin/users/show/{id}", method = RequestMethod.GET)
    public String showUser(@PathVariable("id") Integer userId, ModelMap modelMap) {
        UserEntity userEntity = userService.queryUserById(userId);
        modelMap.addAttribute("user", userEntity);
        return "admin/userDetail";
    }


    /**
     * 修改用户信息
     * 2016/10/11  17:05
     */
    @RequestMapping(value = "/admin/users/update/{id}", method = RequestMethod.GET)
    public String updateUser(@PathVariable("id") Integer userId, ModelMap modelMap) {
        UserEntity userEntity = userService.queryUserById(userId);// userRepository.findOne(userId);
        modelMap.addAttribute("user", userEntity);
        return "admin/updateUser";
    }

    /**
     * 修改用户信息POST提交
     * 2016/10/11  15:53
     */
    @RequestMapping(value = "/admin/users/updateP", method = RequestMethod.POST)
    public String updateUserPost(@ModelAttribute("userP") UserEntity user) {
        //更新用户信息
        userService.updateUser(user);
        return "redirect:/admin/users";
    }

    /**
     * 删除用户
     * 2016/10/11  17:09
     */
    @RequestMapping(value = "/admin/users/delete/{id}", method = RequestMethod.GET)
    public String deleteUser(@PathVariable("id") Integer userId) {
        userService.deleteUserById(userId);
        log.logger.error("删除一个用户Id:" + userId);
        return "redirect:/admin/users";
    }

    @RequestMapping(value = "/admin/users/test", method = RequestMethod.GET)
    public String Test() {
            return "admin/test";
    }

}


